package com.wing.sys.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.sys.dao.ModuleDao;
import com.wing.sys.dao.RoleModuleDao;
import com.wing.sys.domain.Module;
/**
 * 
 * @author please write yout name
 * 
 */
@Service
@Transactional
public class ModuleManager {
	@Autowired
	private ModuleDao moduledao;
	@Autowired
	private RoleModuleDao roleModuleDao;
	
	public Module findById(String id){
		return moduledao.findById(id);	 
	}
	public List<Map<String,Object>> findAll(){
		return moduledao.findAll();	 
	}
	
	public PageBean findPageList(int currentPage, int pageSize, String moduleId) {
		return moduledao.findPageList(currentPage, pageSize, moduleId);
	}
	
	public void saveModule(Module m, String ptree_id) {
		if(m.getId() == null ||  "".equals(m.getId())){
			m.setId(new GUID().toString());
			ptree_id += ";"+m.getId();
			m.setTree_id(ptree_id);
			moduledao.insert(m);
		}else{
			moduledao.update(m);
		}
	}

	public void updateModule(Module m){
		moduledao.update(m);
	}
	

	public int deleteModules(String[] chk) {
		int count = 0;
		for(int i=0;i<chk.length;i++){
			count+=deleteModule(chk[i]);
		}
		return count;
	}
	
	/**
	 * 递归删除moduleId下的所有的模块
	 * @param moduleId
	 */
	public int deleteModule(String moduleId){
		int count=0;
		List<Map<String, Object>> list = moduledao.findChildByID(moduleId,false,null);
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			count+=deleteModule((String)map.get("id"));
		}
		roleModuleDao.deleteRoleModuleByModuleId(moduleId);
		moduledao.delete(moduleId);
		count++;
		return count;
	}
	
	/********************************获取模块树的xml开始********************************/
	
	public String createDHTMLXML(String parentid, boolean firstFlag, boolean ismenu,
			String userId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		if ((parentid == null || parentid.equalsIgnoreCase(Module.ROOT_MODULE)) && firstFlag) {
			Module rootModule = moduledao.findById(parentid);
			buffer.append("<tree id=\"0\">");
			buffer.append("<item text=\""+rootModule.getName()+"\" id=\""+rootModule.getId()+"\" open=\"1\" select=\"1\">");
		} else {
			buffer.append("<tree id=\"" + parentid + "\">");
		}
		// 循环读取所有节点 
		List<Map<String, Object>> list = moduledao.findChildByID(parentid,ismenu,userId);
		for (Map map : list) {
			String id = (String)map.get("id");
			String name = (String)map.get("name");
			buffer.append("<item text= \"" + name + "\"");
			buffer.append(" id= \"" + id + "\"");

			if (moduleIsLeaf(id,ismenu,userId)) {
				buffer.append(" im0=\"iconText.gif\" im1=\"iconTexts.gif\" im2=\"iconText.gif\"");
				buffer.append(" child=\"0\" >");
			} else {
				buffer.append(" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\"");
				buffer.append(" child=\"" + id + "\">");
			}
			buffer.append("</item>");
		}
		if ((parentid == null || parentid.equalsIgnoreCase(Module.ROOT_MODULE)) && firstFlag) {
			buffer.append("</item>");
		}
		buffer.append("</tree>");
		return buffer.toString();
	}
	
	public String createDHTMLXML(String parentid,boolean firstFlag,boolean ismenu) {
		return createDHTMLXML(parentid,firstFlag,ismenu,null);
	}
	

	/**
	 * 判断该模块是否是叶子节点
	 * @param Organization Id
	 * @return 
	 */
	private boolean moduleIsLeaf(String id,boolean ismenu,String userId){
		List list = moduledao.findChildByID(id,ismenu,userId);
		return list.size()<=0;
	}
	/********************************获取模块树的xml结束********************************/
	


	
}



